var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs");
var port = 26737;
var host = 'localhost';

var data = '{"name": "zhan", "age": "23"}';
http.createServer(function(req, res) {
    console.log(req.url);
    console.log(url.parse(req.url));
    sse(req, res);

    switch (req.url) {
        case '/a.js':
        getA(req, res);
        break;
        case '/b.js':
        getSSE(req, res);
        break;
        default:
        getHTML(req, res);
    }
}).listen(port, host);

function sse(req, res) {
    var fileName;
    var interval;
    // console.log("req.url:" + req.url);
    fileName = "." + req.url;
    // console.log("fileName:" + fileName);

    if (fileName === "./stream") {
        res.writeHead(200, {"Content-Type":"text/event-stream", "Cache-Control":"no-cache", "Connection":"keep-alive"});
        res.write("retry: 1000\n");
        res.write("event: connecttime\n");
        res.write("data: " + (new Date()) + "\n\n");
        res.write("data: " + (new Date()) + "\n\n");

        interval = setInterval(function() {
          res.write("data: " + (new Date()) + "\n\n");
      }, 1000);

        req.connection.addListener("close", function () {
          clearInterval(interval);
      }, false);
    } 
}

function getA(req, res) {
    res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"http://localhost"});
    var json = data;
    // res.write("你好啊!");
    res.end(json);
}

function getHTML(req, res) {
    var pathname = __dirname + url.parse(req.url).pathname;
    if (path.extname(pathname) == "") {
        pathname += "/";
    }
    if (pathname.charAt(pathname.length - 1) == "/") {
        pathname += "index.html";
    }

    if (path.isAbsolute(pathname)) {
        switch (path.extname(pathname)) {
            case ".html":
            res.writeHead(200, {
                "Content-Type": "text/html"
            });
            break;
            case ".js":
            res.writeHead(200, {
                "Content-Type": "text/javascript"
            });
            break;
            case ".css":
            res.writeHead(200, {
                "Content-Type": "text/css"
            });
            break;
            case ".gif":
            res.writeHead(200, {
                "Content-Type": "image/gif"
            });
            break;
            case ".jpg":
            res.writeHead(200, {
                "Content-Type": "image/jpeg"
            });
            break;
            case ".png":
            res.writeHead(200, {
                "Content-Type": "image/png"
            });
            break;
            case ".pdf":
            res.writeHead(200, {
                "Content-Type": "application/pdf"
            });
            break;
            case ".php":
            res.writeHead(200, {
                "Content-Type": "text/x-php"
            });
            break;
            default:
            res.writeHead(200, {
                "Content-Type": "application/octet-stream"
            });
        }

        fs.readFile(pathname, function(err, data) {
            res.end(data);
        });
    } else {
        res.writeHead(404, {
            "Content-Type": "text/html"
        });
        res.end("<h1>404 Not Found</h1>");
    }
}

console.log('Server running at ' + host + ':' + port );